package com.cloud.school.bus.teacherhelper.base.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Environment;

import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.utils.Base64Util;
import com.android.support.jhf.utils.ImageUtil;
import com.android.support.jhf.utils.SDCardToolUtil;
import com.android.support.jhf.utils.ThumbnailUtils;
import com.cloud.school.bus.teacherhelper.entitys.Picture;

public class PictureUtil {

	private static LinkedList<String> extens = null;

	public static LinkedList<String> getExtens() {
		if (extens == null) {
			extens = new LinkedList<String>();
			extens.add("JPEG");
			extens.add("JPG");
			extens.add("PNG");
			extens.add("GIF");
			extens.add("BMP");
		}
		return extens;
	}
	
	private static LinkedList<String> extensVideo = null;
	
	public static LinkedList<String> getVideoExtens() {
		if (extensVideo == null) {
			extensVideo = new LinkedList<String>();
			extensVideo.add("MP4");
		}
		return extensVideo;
	}

	public static List<Picture> getFiles(String root, List<Picture> ignore) {
		File f = new File(root);

		File[] files = f.listFiles();
		List<Picture> pictures = new ArrayList<Picture>();
		if (files != null) {
			for (int i = 0; i < files.length; i++) {
				final File ff = files[i];
				if (ff.isDirectory()) {
					pictures.addAll(0,getFiles(ff.getPath(), ignore));
				} else {
					String fName = ff.getName();
					if (fName.indexOf(".") > -1) {
						String end = fName.substring(
								fName.lastIndexOf(".") + 1, fName.length())
								.toUpperCase();
						if (getExtens().contains(end)) {
							Picture picture = new Picture();
							picture.setPicturePath(ff.getPath());
							
							//临时的用于过滤忽略的
							Picture tempPicture = new Picture();
							tempPicture.setPicturePath(Uri.decode(Uri.fromFile(new File(ff.getPath())).toString()));
							
							if (!ignore.contains(tempPicture)) {
								pictures.add(0,picture);
							}
						}
					}
				}
			}
		}
		return pictures;
	}
	
	public static List<Picture> getVideoFiles(String root, List<Picture> ignore) {
		File f = new File(root);

		File[] files = f.listFiles();
		List<Picture> pictures = new ArrayList<Picture>();
		if (files != null) {
			for (int i = 0; i < files.length; i++) {
				final File ff = files[i];
				if (ff.isDirectory()) {
					pictures.addAll(0,getFiles(ff.getPath(), ignore));
				} else {
					String fName = ff.getName();
					if (fName.indexOf(".") > -1) {
						String end = fName.substring(
								fName.lastIndexOf(".") + 1, fName.length())
								.toUpperCase();
						if (getVideoExtens().contains(end)) {
							Picture picture = new Picture();
							picture.setPicturePath(ff.getPath());
							
//							//临时的用于过滤忽略的
							Picture tempPicture = new Picture();
							tempPicture.setPicturePath(Uri.decode(Uri.fromFile(new File(ff.getPath())).toString()));
							if (!ignore.contains(tempPicture)) {
								pictures.add(0,picture);
							}
							
//							if (!ignore.contains(picture)) {
//								pictures.add(picture);
//							}
						}
					}
				}
			}
		}
		return pictures;
	}
	
	/**
	 * 获取照片byte转换成base64编码的String
	 * @param picPathString
	 * @param imageBound
	 * @return
	 */
	public static String getPicString(String picPathString, int imageBound) {
		String datePicString = null;
		try {
			int[] imageBounds = ImageUtil
					.getImageBounds(new File(picPathString));
			DebugLog.logI("imageBounds : " + imageBounds[0]);
			Bitmap bitmap = null;
			if (imageBounds[0] > imageBound) {
				bitmap = ThumbnailUtils.setThumbnailBitmap(new File(
						picPathString), imageBound, imageBound);
			} else {
				bitmap = BitmapFactory.decodeFile(picPathString);
			}

			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			bitmap.compress(CompressFormat.JPEG, 100, byteArrayOutputStream);

			datePicString = Base64Util.encode(byteArrayOutputStream
					.toByteArray());

			DebugLog.logI("datePicString : " + datePicString.length());
			DebugLog.logI("byteArrayOutputStream : "
					+ byteArrayOutputStream.toByteArray().length);
			byteArrayOutputStream.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return datePicString;
	}

//	/**
//	 * 获取sd卡中所有文件
//	 * 
//	 * @param context
//	 * @return
//	 */
//	public static List<Picture> getAllPictures(Context context) {
//
//		List<Picture> pictures = new ArrayList<Picture>();
//		if (SDCardToolUtil.isExistSDCard()) {
//			pictures.addAll(getFiles(Environment.getExternalStorageDirectory()
//					.getAbsolutePath()));
//		} else {
//			HandlerToastUI.getHandlerToastUI(context, "请插入sd卡");
//		}
//
//		return pictures;
//	}

}
